package com.bupt.smart.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.bupt.smart.application.CustomApplication;
import com.bupt.smart.model.Delivery;
import com.bupt.smart.network.APIManager;
import com.bupt.smart.smart.R;
import com.bupt.smart.ui.adapter.DeliveryRecyclerViewAdapter;
import com.bupt.smart.utils.CheckNetwork;
import com.bupt.smart.utils.SaveSharedPreference;
import com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by ght on 2016/6/20.
 */
public class DeliveryPageFragment extends Fragment {
    public static final String ARG_PAGE = "ARG_PAGE";
    public static final int PAGE_DELIVERY_UNCLAIMED = 0;
    public static final int PAGE_DELIVERY_CLAIMED = 1;

    private int tailPage = 1;   //尾页页码
    private static final int pageSize = 10;  //default pageSize
    private int mStatus = 0;    //快递收取状态(0:未签收，1:已签收)

    private PullLoadMoreRecyclerView mRecyclerView;
    private DeliveryRecyclerViewAdapter mRecyclerViewAdapter;

    private List<Delivery> data = new ArrayList<Delivery>();

    public static DeliveryPageFragment newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        DeliveryPageFragment fragment = new DeliveryPageFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mStatus = getArguments().getInt(ARG_PAGE);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.delivery_history, null);
        initview(v);
        initData();
        return v;
    }

    private void initview(View v) {
        mRecyclerView = (PullLoadMoreRecyclerView) v.findViewById(R.id.deliveryHistoryRecyclerView);
        mRecyclerView.setLinearLayout();

        mRecyclerViewAdapter = new DeliveryRecyclerViewAdapter(getContext(), data);
        mRecyclerView.setAdapter(mRecyclerViewAdapter);

        mRecyclerView.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {
            @Override
            public void onRefresh() {
                if (!CheckNetwork.isConnected(getContext())) {
                    Toast.makeText(getActivity(), getString(R.string.network_not_connected), Toast.LENGTH_SHORT).show();
                } else {

                    //启动异步任务进行快递查询操作
                    CustomApplication customApplication = (CustomApplication) getActivity().getApplication();
                    Call<List<Delivery>> call = APIManager.getDeliveryAPI().getDeliveryHistory(SaveSharedPreference.
                            getUserInfo(DeliveryPageFragment.this.getContext()).getUserID(),
                            SaveSharedPreference.getUserInfo(DeliveryPageFragment.this.getContext()).getPhone(),
                            mStatus, 1, pageSize);
                    call.enqueue(new Callback<List<Delivery>>() {
                        @Override
                        public void onResponse(Call<List<Delivery>> call, Response<List<Delivery>> response) {
                            mRecyclerView.setPullLoadMoreCompleted();
                            if (response.isSuccessful()) {
                                data.clear();
                                data.addAll(response.body());
                                mRecyclerViewAdapter.notifyDataSetChanged();
                            } else {
                                Toast.makeText(getActivity(), response.code() + "", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<List<Delivery>> call, Throwable t) {
                            mRecyclerView.setPullLoadMoreCompleted();
                            Toast.makeText(getActivity(), getString(R.string.error), Toast.LENGTH_SHORT).show();
                            t.printStackTrace();
                        }
                    });
                }
            }

            @Override
            public void onLoadMore() {
                if (!CheckNetwork.isConnected(getContext())) {
                    Toast.makeText(getActivity(), getString(R.string.network_not_connected), Toast.LENGTH_SHORT).show();
                } else {
                    //启动异步任务加载更多
                    CustomApplication customApplication = (CustomApplication) getActivity().getApplication();
                    Call<List<Delivery>> call = APIManager.getDeliveryAPI().getDeliveryHistory(SaveSharedPreference.
                                    getUserInfo(DeliveryPageFragment.this.getContext()).getUserID(),
                            SaveSharedPreference.getUserInfo(DeliveryPageFragment.this.getContext()).getPhone(),
                            mStatus, tailPage + 1, pageSize);
                    call.enqueue(new Callback<List<Delivery>>() {
                        @Override
                        public void onResponse(Call<List<Delivery>> call, Response<List<Delivery>> response) {
                            mRecyclerView.setPullLoadMoreCompleted();
                            if (response.isSuccessful()) {
                                data.addAll(response.body());
                                mRecyclerViewAdapter.notifyDataSetChanged();
                                tailPage++;
                            } else {
                                Toast.makeText(getActivity(), response.code() + "", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<List<Delivery>> call, Throwable t) {
                            mRecyclerView.setPullLoadMoreCompleted();
                            Toast.makeText(getActivity(), getString(R.string.error), Toast.LENGTH_SHORT).show();
                            t.printStackTrace();
                        }
                    });
                }
            }
        });
    }

    private void initData() {
        mRecyclerView.refresh();
    }
}
